//
//  Sheetsbootcamp.swift
//  FCSwiftUI
//
//  Created by 冯宣超 on 2023/2/24.
//

import SwiftUI

struct Sheetsbootcamp: View {
    
    @State var showSheet: Bool = false
    @State var showSecond: Bool = false
    
    var body: some View {
        ZStack {
            Color.green.edgesIgnoringSafeArea(.all)
            
            VStack(spacing: 20) {
                Button {
                    showSheet.toggle()
                } label: {
                    Text("Button Sheet")
                        .foregroundColor(.green)
                        .font(.headline)
                        .padding(20)
                        .frame(width: 180)
                        .background(Color.white.cornerRadius(10))
                }
                .sheet(isPresented: $showSheet) {
                    Text("Hello, There!!!!")
                }
                
                Button {
                    showSecond.toggle()
                } label: {
                    Text("Button Second")
                        .foregroundColor(.green)
                        .font(.headline)
                        .padding(20)
                        .frame(width: 180)
                        .background(Color.white.cornerRadius(10))
                }
//                .sheet(isPresented: $showSecond) {
//                    // Do Not Add Conditional Here(这个闭包内不能添加条件判断)
//                    SecondScreen()
//                }
                .fullScreenCover(isPresented: $showSecond) {
                    SecondScreen()
                }
            }
        }
    }
}

private struct SecondScreen: View {
    
//    @Environment(\.presentationMode) var presentationMode
    @Environment(\.dismiss) var dismiss
    
    var body: some View {
        ZStack(alignment: .topLeading) {
            Color.red.edgesIgnoringSafeArea(.all)
            
            Button {
//                presentationMode.wrappedValue.dismiss()
                dismiss()
            } label: {
                Image(systemName: "xmark")
                    .foregroundColor(.white)
                    .font(.largeTitle)
                    .padding(20)
     
            }
        }
    }
}

struct Sheetsbootcamp_Previews: PreviewProvider {
    static var previews: some View {
        Sheetsbootcamp()
        //SecondScreen()
    }
}
